package com.moli.iov.handle.alarm.biz.real;

import com.moli.iov.base.enums.AlarmAuthEnum;
import com.moli.iov.handle.alarm.enums.AlarmCodeEnum;
import com.moli.iov.handle.alarm.enums.AlarmSourceEnum;
import com.moli.iov.handle.alarm.enums.AlarmTypeEnum;
import com.moli.iov.handle.alarm.enums.AntiShakeEnum;
import com.moli.iov.handle.alarm.model.dto.AlarmDto;
import com.moli.iov.handle.alarm.model.dto.AuthDto;
import com.moli.iov.handle.alarm.model.dto.DisarmAlarmDto;
import com.moli.iov.handle.alarm.service.IAlarmInfoRequestService;
import com.moli.iov.protocol.enums.realtime.GeneralAlarmStateTag;
import com.moli.iov.protocol.model.realtime.AlarmReq;
import com.moli.iov.protocol.model.realtime.RealTimeInfoReq;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  实时普通报警
 * </p>
 *
 * @author: zql
 * @Date: 2019/2/15 9:26
 */
@Slf4j
@Component
public class GeneralAlarmReal {

    @Autowired
    private IAlarmInfoRequestService alarmInfoRequestService;

    public void handle(RealTimeInfoReq realTimeInfoReq, AuthDto authDto){
        try{
            AlarmReq alarmReq = realTimeInfoReq.getAlarmReq();
            GeneralAlarmStateTag generalAlarmStateTag = alarmReq.generalAlarmState;
            if(generalAlarmStateTag == null){
                log.error("实时普通报警处理失败，generalAlarmStateTag为Null");
                return;
            }

            Map<Byte, GeneralAlarmStateTag.IsAlarm> generalAlarms = generalAlarmStateTag.getVOfList();
            if(generalAlarms == null){
                log.error("实时普通报警处理失败，获取报警Map失败 vin:{}", realTimeInfoReq.vin);
                return;
            }

            subHandle(realTimeInfoReq.vin, generalAlarms, realTimeInfoReq, authDto);

        }catch(Exception e){
            log.error("实时普通报警异常", e);
        }
    }


    public void subHandle(final String vin, Map<Byte,
                          GeneralAlarmStateTag.IsAlarm> generalAlarms,
                          RealTimeInfoReq realTimeInfoReq,
                          AuthDto authDto){
        generalAlarms.forEach((alarmCode, isAlarm) -> {
            try{
                if(GeneralAlarmStateTag.IsAlarm.YES == isAlarm){

                    if(authDto.getAlarmAuths().contains(AlarmAuthEnum.REAL.getFlag())){
                        log.info("实时普通报警 不监控报警 vin:{}", vin);
                        return;
                    }

                    log.info("实时普通报警 报警 vin:{} 报警类型：{}",
                            vin,
                            AlarmCodeEnum.getMsg(alarmCode));
                    double longitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.longitude : 0;
                    double latitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.latitude : 0;

                    alarmInfoRequestService.alarm(new AlarmDto(
                            vin,
                            AlarmCodeEnum.getEnum((int)alarmCode),
                            AlarmTypeEnum.ALARM.getFlag(),
                            AlarmSourceEnum.T_BOX.getFlag(),
                            "",
                            AntiShakeEnum.YES.getFlag(),
                            longitude,
                            latitude));

                }else if(GeneralAlarmStateTag.IsAlarm.NO == isAlarm){

                    alarmInfoRequestService.disarmAlarm(new DisarmAlarmDto(vin,
                            (int)alarmCode,
                            AntiShakeEnum.YES.getFlag()));

                }else{
                    log.error("实时普通报警处理失败 未定义的报警状态 vin:{} 报警类型：{} 报警结果:{}",
                            vin, GeneralAlarmStateTag.AlarmType.getKey(alarmCode), isAlarm);
                }
            }catch(Exception e){
                log.error("实时普通报警处理异常", e);
            }
        });
    }

}
